package com.water.camera.mark;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.water.camera.bean.EditItem;
import com.water.camera.databinding.LayoutWatermark29Binding;
import com.water.camera.util.Utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class Mark29 extends BaseMark {

    LayoutWatermark29Binding binding;

    public Mark29(@NonNull Context context) {
        super(context);
    }

    public Mark29(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public Mark29(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(int type) {
        if (type == 0) {
            binding = LayoutWatermark29Binding.inflate(LayoutInflater.from(getContext()));
            addView(binding.getRoot());
        } else {
            binding = LayoutWatermark29Binding.inflate(LayoutInflater.from(getContext()), this, true);
        }
        set = new HashSet<>();
        set.add(binding.ivL.getId());
        set.add(binding.ivR.getId());
        set.add(binding.tvTitle.getId());
        set.add(binding.ivLoc.getId());
        set.add(binding.tvLocation.getId());
        update();
        if (type == 0) {
            hideView(binding.ivL, binding.tvTitle, binding.ivR, binding.ivLoc, binding.tvLocation);
        }
    }

    @Override
    public void update() {
        long time = getTime();
        if (time > 0) {
            String[] ss = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(time).split("\\.");
            binding.tvYears.setText(ss[0]);
            binding.tvMonth.setText(ss[1]);
            binding.tvDay.setText(ss[2]);
            binding.tvHours.setText(ss[3]);
            binding.tvMil.setText(ss[4]);
            binding.tvSeconds.setText(ss[5]);
        }
        setText(binding.tvTitle, "title29", binding.tvTitle);
        setText(binding.tvLocation, "didian29", binding.tvLocation);

        super.update();
    }

    @Override
    public ArrayList<EditItem> getEditItems() {
        ArrayList<EditItem> list = new ArrayList<>();
        list.add(new EditItem("time29", "时间", true));
        list.add(new EditItem("title29", "标题"));
        list.add(new EditItem("didian29", "地点"));
        return list;
    }

    @Override
    public void onValueChange(EditItem item) {
        if (item.isTime) {
            long time = getTime();
            if (time > 0) {
                String[] ss = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(time).split("\\.");
                binding.tvYears.setText(ss[0]);
                binding.tvMonth.setText(ss[1]);
                binding.tvDay.setText(ss[2]);
                binding.tvHours.setText(ss[3]);
                binding.tvMil.setText(ss[4]);
                binding.tvSeconds.setText(ss[5]);
            }
        } else {
            super.onValueChange(item);
        }
    }

    HashSet<Integer> set;

    @Override
    public void onShowChange(EditItem item) {
        switch (item.key) {
            case "time29": {
                int n = binding.group.getChildCount();
                for (int i = 0; i < n; i++) {
                    View v = binding.group.getChildAt(i);
                    if (!set.contains(v.getId())) {
                        if (item.show) {
                            showView(v);
                        } else {
                            hideView(v);
                        }
                    }
                }
                break;
            }
            case "title29": {
                if (item.show) {
                    showView(binding.ivL, binding.tvTitle, binding.ivR);
                } else {
                    hideView(binding.ivL, binding.tvTitle, binding.ivR);
                }
                break;
            }
            case "didian29": {
                if (item.show) {
                    showView(binding.ivLoc, binding.tvLocation);
                } else {
                    hideView(binding.ivLoc, binding.tvLocation);
                }
                break;
            }
        }
    }
}
